import os
dirct = "F:/PATISS/work/"
dirList = []
fileList = []
files = os.listdir(dirct)  # 文件夹下所有目录的列表
for f in files:
    if os.path.isdir(dirct + '/' + f):  # 这里是绝对路径，该句判断目录是否是文件夹
        dirList.append(f)
    elif os.path.isfile(dirct + '/' + f):  # 这里是绝对路径，该句判断目录是否是文件
        fileList.append(f)
x = 0
for x in range(len(fileList)):
    print("文件有：", dirList[x])
    dirPath = dirct + dirList[x]
    # 所有txt位于的文件夹路径
    files = os.listdir(dirPath)
    res = ""
    i = 0
    for file in files:
        if file.endswith(".txt"):
            i += 1
            # title = "第%s章 %s" % (i, file[0:len(file)-4]) # 以文件名为章节名
            title = "第%s章" % (i)  # 以数字为章节名
            with open(dirct + dirList[x] + "/" + file, "r", encoding='utf-8') as file:
                content = file.read()
                file.close()

            append = "\n%s\n\n%s" % (title, content)
            res += append

    with open(dirct + dirList[x] + ".txt", "w", encoding='utf-8') as outFile:
        outFile.write(res)
        outFile.close()
    print(len(res))
    print("文件《%s》已完成：" % (dirList[x]))
    x += 1
